<template>
	<view  class="content">
	  <!-- 导航栏 -->
	  <Navbar></Navbar>
	  <scroll-view
	   scroll-y 
	   class="main" 
	   @scrolltolower="onScrollTolower"
	   @refresherrefresh="onReRresherRefresh"
       refresher-enabled
       :refresher-triggered="refresherTriggered"
	   >
		<!-- 广告 -->
		<Carousel :banners="banners" height="280rpx"></Carousel>

		<!-- 前台类目 -->
		<CateScroll :categoryHeadMutli="categoryHeadMutli"></CateScroll>

		<!-- 人气推荐 -->
        <view class="panel recommend">
			<view class="item" v-for="item in hotMutli" :key="item.id">
				<view class="title">
					{{ item.title }}<text>{{ item.alt }}</text>
				</view>
				<navigator
				  hover-class="none"
				  :url="`/pages/recommend/index?type=${item.type}`"
				  class="cards"
				>
				  <image
					mode="aspectFit"
					v-for="img in item.pictures"
					:key="img"
					:src="img"
				  ></image>
				</navigator>
			</view>
        </view>
		<!-- 新鲜好物 -->
		<view class="panel fresh">
			<view class="title">
			新鲜好物
			<navigator
				hover-class="none"
				class="more"
				url="/pages/recommend/index?type=5"
				>更多</navigator
			>
			</view>
			<view class="cards">
			<navigator
				hover-class="none"
				:url="`/pages/goods/index?id=${item.id}`"
				v-for="item in homeNew"
				:key="item.id"
			>
				<image mode="aspectFit" :src="item.picture"></image>
				<view class="name">{{ item.name }}</view>
				<view class="price">
				<text class="small">¥</text>{{ item.price }}
				</view>
			</navigator>
			</view>
		</view>

		<!-- 热门品牌 -->
		<view class="panel brands">
			<view class="title">
			热门品牌
			<navigator hover-class="none" class="more" url="/pages/list/index"
				>更多</navigator
			>
			</view>
			<view class="cards">
			<navigator hover-class="none" url="/pages/goods/index">
				<image
				mode="aspectFit"
				src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
				></image>
				<view class="name">小米</view>
				<view class="price">99元起</view>
			</navigator>
			<navigator hover-class="none" url="/pages/goods/index">
				<image
				mode="aspectFit"
				src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
				></image>
				<view class="name">TCL</view>
				<view class="price">199起</view>
			</navigator>
			<navigator hover-class="none" url="/pages/goods/index">
				<image
				mode="aspectFit"
				src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
				></image>
				<view class="name">饭小宝</view>
				<view class="price">9.9起</view>
			</navigator>
			<navigator hover-class="none" url="/pages/goods/index">
				<image
				mode="aspectFit"
				src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
				></image>
				<view class="name">鳄鱼</view>
				<view class="price">299起</view>
			</navigator>
			</view>
		</view>
		<!-- 猜你喜欢 -->
      	<Guess :homeGoodsGuessLike="homeGoodsGuessLike" :hasMore="hasMore"></Guess>

	  </scroll-view>
  </view>
</template>

<script>
// 引入自定义组件 因为不符合eschar的规范 需手动引入和注册
import Navbar from "./components/Navbar.vue"
// 引入接口请求
import http from "@/utils/http"
// 引入 封装好发送请求的代码
import { getHomeBanner,getHomeCategoryHeadMutli,getHomeHotMutli,getHomeNew,getHomeGoodsGuessLike  } from "@/http/home";

export default {
	components:{Navbar},
	data () {
	  return {
	    // 首页-广告区域
		banners: [],
		// 首页-前台类目-小程序
        categoryHeadMutli: [],
		// 人气推荐
      	hotMutli: [],
		// 新鲜好物
      	homeNew: [],
		// 猜你喜欢
		homeGoodsGuessLike:[],
		hasMore:true,// 是否还有下一页
		// 控制下拉刷新的状态
      	refresherTriggered: false,
		// data:{
		// 	page: 1,// 页码
        // 	pageSize: 50,// 页大小
		// },
      	// guessTotalPages: 1,// 总页数
	  }
	},
	computed:{
	  text(){
		return this.$store.state.text
	  }
	},
	onLoad() {
	    this.loadData();
    },
	methods: {
		loadData() {
			// 声明一个全局变量，存放 获取 猜你喜欢的参数
			this.data = {
				page: 1,
				pageSize: 20,
			};
			this.guessTotalPages = 1;
			// Promise.all里面函数是同时执行的  各自请求成功，各自完成赋值 不存在函数1等待函数2执行的情况！！！
			// Promise.all()返回值也是promise,所以才可以.then往下去调用
			return Promise.all([
			this.getHomeBanner(),
			this.getHomeCategoryHeadMutli(),
			this.getHomeHotMutli(),
			this.getHomeNew(),
			this.getHomeGoodsGuessLike(this.data)
			])
		},
		// 只要在函数前面加上一个 async 来修饰，返回值一定是promise
		async getHomeBanner(){
			const result = await getHomeBanner();
			this.banners = result.result;
			console.log(2);
		},
		async getHomeCategoryHeadMutli(){
			const result = await getHomeCategoryHeadMutli();
			this.categoryHeadMutli = result.result;
			console.log(3);
		},
		async getHomeHotMutli(){
			const result = await getHomeHotMutli();
			this.hotMutli = result.result;
			console.log(4);
		},
		async getHomeNew(){
			const result = await getHomeNew();
			this.homeNew = result.result;
			console.log(5);
		},
		async getHomeGoodsGuessLike(data){
			// 猜你喜欢的  请求 
			const result = await getHomeGoodsGuessLike(data);
			// 给总页数赋值
			this.guessTotalPages = result.result.pages;
			// 给猜你喜欢 数组赋值
			this.goodsGuesslike = result.result.items;
		},
		// scroll-view 滚动条触底事件
		async onScrollTolower(){
			// console.log(142,"到底了");
			// 判断 有没有下一页数据   当前页码和总页数 比较
			if (this.data.page >= this.guessTotalPages) {
			// 没有下一页数据了
			uni.showToast({
				title: '没有更多数据了',
				icon: 'none',
				mask: true
			});
			this.hasMore = false;
			} else {
			// 有下一页数据
			this.data.page++;
			// 重新请求
			const result = await getHomeGoodsGuessLike(this.data);
			// 数组合并
			this.homeGoodsGuessLike = [...this.homeGoodsGuessLike, ...result.result.items];
			}
		},
		// 下拉刷新
		async onReRresherRefresh() {
			console.log("下拉刷新了");
			// 设置 下拉刷新状态
			this.refresherTriggered = true;
			// 业务代码
			this.banners = [];
			this.categoryHeadMutli = [];
			this.hotMutli = [];
			this.homeNew = [];
			this.goodsGuesslike = [];
			this.hasMore = true;

			// 下面两段代码 loadData()中有重置的效果了
			// this.data.page = 1;
			// this.guessTotalPages = 1;
			// 业务代码

			// 只使用async await 不能解决
			// console.log(1);
			// await this.loadData();// 这个是异步的 使用async await 也解决不了根本的 数据回来比结束加载慢的问题
			// // 保证数据都回来 才执行
			// console.log(6); // 使用async await 的打印结果为 1 6 (2 5 4 3) 而非 1 (2 5 4 3) 6
			// this.refresherTriggered = false;

			// 使用Promise.all() + async await 能解决
			console.log(1);
			await this.loadData();
			
			// 保证数据都回来 才执行
			console.log(6); // 打印结果为  1 (2 5 4 3) 6
			this.refresherTriggered = false;

		},
	},
}
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>